package com.vimi8.ebb.auth.utils;


import com.vimi8.ebb.auth.dto.MessageClassDTO;
import com.vimi8.ebb.auth.model.MessageQuery;
import com.vimi8.ebb.auth.model.MyMessage;
import com.vimi8.ebb.auth.service.MyMessageService;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.UUID;

/**
 * Created by wangguiyin on 2016/12/22.
 */
public class MessageUtil {


    /**
     * 对客户端的参数进行处理 插入消息。
     */
    public static MyMessage dealMessageTags(String releaseId, MessageQuery messageQuery, MyMessageService myMessageService){
        Map<String,Object> messMap = messageQuery.getMessMap();
        String messClass=messMap.get("messClass").toString();
        MyMessage message = new MyMessage();
        MessageClassDTO messageClassDTO=myMessageService.getMessageClassTemplate(messClass);//获得消息模板
        //将模板信息赋值到MyMessage
        message.setId(UUID.randomUUID().toString());
        message.setMessageTitle(messageClassDTO.getMessTitle());
        message.setMessageType(messageClassDTO.getMessType());
        message.setMessageClass(messageClassDTO.getMessClass());
        message.setReceiveId(messMap.get("receiveId").toString());
        message.setReleaseId(releaseId);
        if(messMap.containsKey("messageRecordId")&&StringUtils.isEmpty(messMap.get("messageRecordId").toString())){
            message.setMessageRecordId(messMap.get("messageRecordId").toString());
        }

        /**
         * XX:表示用户姓名
         * FF:表示农场名称
         * PP：表示产品名称
         * */
        String XX=(messMap.containsKey("XX")&&!StringUtils.isEmpty(messMap.get("XX")))?messMap.get("XX").toString():"";
        String FF=(messMap.containsKey("FF")&&!StringUtils.isEmpty(messMap.get("FF")))?messMap.get("FF").toString():"";
        String PP=(messMap.containsKey("PP")&&!StringUtils.isEmpty(messMap.get("PP")))?messMap.get("PP").toString():"";
        if("create_farm".equals(messClass)){
            messageClassDTO.setMessContent(messageClassDTO.getMessContent().replace("XX",XX));
        }
        if("create_success".equals(messClass)||"create_fail".equals(messClass)){
            messageClassDTO.setMessContent(messageClassDTO.getMessContent().replace("FF",FF));
        }
        if("real_name_auth".equals(messClass)||"auth_success".equals(messClass)||"auth_fail".equals(messClass)){
            messageClassDTO.setMessContent(messageClassDTO.getMessContent().replace("XX",XX));
        }
        if("join_farm".equals(messClass)||"join_success".equals(messClass)||"join_fail".equals(messClass)){
            messageClassDTO.setMessContent(messageClassDTO.getMessContent().replace("XX",XX));
            messageClassDTO.setMessContent(messageClassDTO.getMessContent().replace("FF",FF));
        }
        if("create_plan".equals(messClass)||"end_plan".equals(messClass)){
            messageClassDTO.setMessContent(messageClassDTO.getMessContent().replace("XX",XX));
            messageClassDTO.setMessContent(messageClassDTO.getMessContent().replace("PP",PP));
        }
        message.setMessageContent(messageClassDTO.getMessContent());

        return message;

    }

//public static void main(String[] args){
//    MessageQuery messageQuery=new MessageQuery();
//    Map<String,Object> messageMap=new HashMap<>();
//    //创建农场接受者消息的是 vimi8运营账号
//    messageMap.put("receiveId","vimi8-001");
//    messageMap.put("messClass","create_success");
//    messageQuery.setMessMap(messageMap);
//   // logger.info("messageQuery="+messageQuery);
//    if(messageMap.containsKey("messageRecordId")&&StringUtils.isEmpty(messageMap.get("messageRecordId").toString())){
//        System.out.print("test"+messageMap.get("receiveId"));
//       // message.setMessageRecordId(messageMap.get("messageRecordId").toString());
//    }
//
//}
}
